#include<unordered_map>
#include<vector>
using namespace std;

class RandomizedSet {
private:
    unordered_map<int, int> map;
    vector<int> vec;
    int cnt = 0;
    int total = 0;

public:
    /** Initialize your data structure here. */
    RandomizedSet() {

    }

    /** Inserts a value to the set. Returns true if the set did not already contain the specified element. */
    bool insert(int val) {
        if (map.find(val) == map.end()) {
            map[val] = cnt++;
            if (total < cnt) {
                ++total;
                vec.push_back(val);
            }
            else
                vec[cnt - 1] = val;
            return true;
        }
        return false;
    }

    /** Removes a value from the set. Returns true if the set contained the specified element. */
    bool remove(int val) {
        if (map.find(val) != map.end()) {
            int index = map[val];
            swap(vec[index], vec[--cnt]);
            map[vec[index]] = index;
            map.erase(val);
            return true;
        }
        return false;
    }

    /** Get a random element from the set. */
    int getRandom() {
        int index = rand() % cnt;
        return vec[index];
    }
};

/**
 * Your RandomizedSet object will be instantiated and called as such:
 * RandomizedSet* obj = new RandomizedSet();
 * bool param_1 = obj->insert(val);
 * bool param_2 = obj->remove(val);
 * int param_3 = obj->getRandom();
 */